package com.ke.hibernate;

import java.util.Date;

import org.hibernate.Transaction;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import com.ke.hibernate.dao.HibernateUtils;
import com.ke.hibernate.domain.Person;

public class Base {

	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

//		Person person = new Person();
//		person.setName("kobe");
//		person.setBirthday(new Date());
//		addPerson(person);
//		System.out.println("id:" + person.getId());
//		Person p = getPerson(person.getId());
//		System.out.println("name:" + p.getName());

	}

	static Person getPerson(int id) {
		Session s = null;
		try {
			s = HibernateUtils.getSession();
			Class personClass = Person.class;
		Person person = (Person) s.get(personClass, id);
//			Person person1 = (Person) s.load(personClass, id);
//			System.out.println(person1.getClass().getName());
			
			return person;

		} finally {
			if (s != null) {
				s.close();

			}
		}

	}

	static void addPerson(Person person) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtils.getSession();
			tx = (Transaction) s.beginTransaction();
			s.save(person);
//			s.persist(person);
			person.setName("kirk");
			tx.commit();

		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			if (s != null) {
				s.close();

			}
		}

	}

	static void addPerson1(Person person) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtils.getSession();
			tx = (Transaction) s.beginTransaction();
			s.save(person);
			tx.commit();

		} finally {
			if (s != null) {
				s.close();

			}

		}

	}

}
